﻿using Aspose.Words;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace LeHouse.Server.WebApi.Common
{
    public class CommonWords
    {
        public static string Word(string TemplateName, string SaveName, List<WordsTemp> dic)
        {
            Document doc = new Document(TemplateName);
            foreach (var key in dic)
            {
                string repStr = string.Format("&{0}&", key.Name);
                if (key.Type == FileFormat.Image.ToString())
                {
                    //DocumentBuilder builder = new DocumentBuilder(doc);
                    ////将光标移动到指定节点
                    //builder.MoveToBookmark(repStr);
                    ////插入图片
                    //builder.InsertImage(dic[key]);
                    Regex reg = new Regex(repStr);
                    var img = new ReplaceAndInsertImage(key.Value);
                    doc.Range.Replace(reg, img, false);
                }
                else
                {
                    doc.Range.Replace(repStr, key.Value, false, false);
                }
            }
            doc.Save(SaveName);//也可以保存为1.doc 兼容03-07
            return SaveName;
        }
    }
}